import pymysql
from pymysql.cursors import DictCursor


class Mysql_handler():
    def __init__(self,host,port,charset,user,password,database):
        #建立链接
        self.conn = pymysql.connect(host=host,port=port,charset=charset,
                                    user=user,password=password,database=database,cursorclass=DictCursor)
        # 每次查询都要一个新游标
        # 发送请求
        self.courser = self.conn.cursor()
    def query(self,sql,args=None,one=True):

        # sql语句
        self.courser.execute(sql,args=args)
        if one:
           return self.courser.fetchone()
        else:
           return self.courser.fetchall()

    def close(self):
        self.conn.close()

if __name__ == '__main__':
    sql = Mysql_handler('127.0.0.1',3306,'utf8','root','764077','futureloan')
    res = sql.query('SELECT * FROM USER WHERE ID = %s',args=['1'])
    sql.close()
    print(res['mobilephone'])

